package com.chushouya.manager.dao.repository;

import com.general.framework.core.enums.DelFlagEnum;
import com.general.framework.core.lang.Integers;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.dao.entity.ClerkEntity;
import com.chushouya.manager.dao.mapper.ClerkMapper;
import com.chushouya.manager.dto.admin.clerk.ClerkQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class ClerkRepository extends BaseRepository<ClerkMapper, ClerkEntity, Long> {

    public PageResult<ClerkEntity> selectPageResult(ClerkQuery query) {
        return this.selectPage(query.getPageRequest(), this.buildExample(query));
    }

    public List<ClerkEntity> selectList(ClerkQuery query) {
        return this.selectByExample(this.buildExample(query));
    }

    public ClerkEntity selectOne(ClerkQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    public ClerkEntity selectOneWithProperties(Long clerkId, String... properties) {
        ClerkQuery query = new ClerkQuery();
        query.setClerkId(clerkId);
        final Example example = this.buildExample(query);
        example.selectProperties(properties);
        return this.selectFirstOneByExample(example);
    }

    private Example buildExample(ClerkQuery query) {
        Example example = buildExample();
        if (Longs.notNullAndZero(query.getClerkId())) {
            example.and().andEqualTo("clerkId", query.getClerkId());
        }
        if (Strings.isNotBlank(query.getClerkPhone())) {
            example.and().andLike("clerkPhone", Strings.format("%{}%", query.getClerkPhone()));
        }
        if (Strings.isNotBlank(query.getClerkName())) {
            example.and().andLike("clerkName", Strings.format("%{}%", query.getClerkName()));
        }
        if (Integers.notNullAndZero(query.getUserType())) {
            example.and().andEqualTo("userType", query.getUserType());
        }
        if (Strings.isNotEmpty(query.getCity())) {
            example.and().andEqualTo("city", query.getCity());
        }
        if (Strings.isNotEmpty(query.getCityCode())) {
            example.and().andEqualTo("cityCode", query.getCityCode());
        }
        if (Objects.nonNull(query.getStatus())) {
            example.and().andEqualTo("status", query.getStatus());
        }
        if (Objects.nonNull(query.getWorkStatus())) {
            example.and().andEqualTo("workStatus", query.getWorkStatus());
        }
        if (Objects.nonNull(query.getPartnerId())) {
            example.and().andEqualTo("partnerId", query.getPartnerId());
        }
        if(Lists.isNotEmpty(query.getUserTypeList())){
            example.and().andIn("userType",query.getUserTypeList());
        }
        if(Objects.nonNull(query.getAutoDispatchStatus())){
            example.and().andEqualTo("autoDispatchStatus",query.getAutoDispatchStatus());
        }
        example.and().andEqualTo("delFlag", DelFlagEnum.normal.getValue());
        return example;
    }

}
